﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ReverseWordsinaStringIII
{
    public class Solution
    {
        public string ReverseWords(string s)
        {
            // 整个句子
            StringBuilder sentence = new StringBuilder();
            // 一个词
            StringBuilder word = new StringBuilder();

            for (int i = 0; i < s.Length; i++)
            {
                var c = s[i];
                if (c == ' ')
                {
                    sentence.Append(word);
                    sentence.Append(' ');
                    word.Clear();
                }
                else
                {
                    word.Insert(0, c);
                }

                if (i == s.Length - 1)
                {
                    sentence.Append(word);
                    word.Clear();
                }
            }

            return sentence.ToString();
        }
    }
}
